@echo off
setlocal ENABLEEXTENSIONS
REM One-click start script for Cool Todo App (ASCII-only output)
cd /d "%~dp0"

echo ========================================
echo        Cool Todo App One-Click Start
echo ========================================
echo.

echo [Step] Check Node.js / npm...
where node >nul 2>&1 || goto ERR_NODE
where npm >nul 2>&1 || goto ERR_NPM

echo [Step] Install dependencies if missing...
if not exist node_modules (
  call npm install
  if errorlevel 1 goto ERR_INSTALL
) else (
  echo [Info] node_modules exists, skip install
)

echo [Step] Build frontend...
call npm run build
if errorlevel 1 goto ERR_BUILD

echo [Step] Start server...
set PORT=5173
set DB_PATH=%CD%\todolist.db
call npm start

goto END

:ERR_NODE
echo [Error] Node.js not detected. Install from: https://nodejs.org/
goto END

:ERR_NPM
echo [Error] npm not detected. Please check Node.js installation
goto END

:ERR_INSTALL
echo [Error] Dependency install failed
goto END

:ERR_BUILD
echo [Error] Frontend build failed
goto END

:END
echo.
echo [Done] Script finished.
pause